package com.jy.appmarket.screenlock.activity;

import com.jy.appmarket.R;
import com.jy.appmarket.screenlock.prefs.LockPrefs;
import com.jy.appmarket.screenlock.utils.XiaomiUtils;
import com.jy.appmarket.screenlock.widget.dialog.SpecialSettingDialog;
import com.jy.appmarket.screenlock.widget.dialog.SpecialSettingDialog.SpecialDialogListener;
import com.umeng.analytics.MobclickAgent;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 类说明：
 * 
 * @author jie.li
 * @date 2015-4-9
 * @version 1.0
 */
public class SpecialSettingActivity extends Activity implements
		OnClickListener{
	private LinearLayout mFloatWindow, mTrust, mSystemLocker;
	private ImageView mFirst, mSecond, mThird;
	private static boolean isMIUI = false;
	private static String mMIUIVersion;
	private boolean isCloseSystemLock = false;
	private boolean isOpenFloatWindow = false;
	private boolean isTrust = false;
	private Button mExperience;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mMIUIVersion = XiaomiUtils.getSystemProperty();
		isMIUI = XiaomiUtils.isMIUI(this);
		setContentView(R.layout.activity_special_setting);
		initView();
	}

	private void initTitle() {
		ActionBar actionBar = getActionBar();
		actionBar.setTitle(R.string.open_xiaomi_setting);
		actionBar.setDisplayUseLogoEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(true);
	}

	private void initView() {
		initTitle();
		mFloatWindow = findViewById(R.id.open_floatwindow_layout);
		mTrust = findViewById(R.id.trust_guide_layout);
		mSystemLocker = findViewById(R.id.close_systemlocker_layout);
		mExperience = findViewById(R.id.experience_btn);

		mFirst = findViewById(R.id.special_setting_first);
		mSecond = findViewById(R.id.special_setting_second);
		mThird = findViewById(R.id.special_setting_third);

		mFloatWindow.setOnClickListener(this);
		mTrust.setOnClickListener(this);
		mSystemLocker.setOnClickListener(this);
		mExperience.setOnClickListener(this);
		mExperience.setClickable(false);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.open_floatwindow_layout:
			isOpenFloatWindow = true;
			mFloatWindow.setSelected(true);
			if (isMIUI) {
				XiaomiUtils.setAllowFolatWindow(this, mMIUIVersion);
				if (mMIUIVersion.equals("V5")) {
					startDialog(1);
//					XiaomiDialogActivity.startXiaomiDialogActivity(this, 1);
				} else if (mMIUIVersion.equals("V6")) {
					startSingleChoiceDialogActivity(getString(R.string.open_floatwindow),getString(R.string.open_floatwindow_up_dialog),getString(R.string.open_floatwindow_dialog));
				} else if (mMIUIVersion.equals("V7")) {
					startSingleChoiceDialogActivity(getString(R.string.open_floatwindow),getString(R.string.open_floatwindow_up_dialog),getString(R.string.open_floatwindow_dialog));
				}
			}

			break;
		case R.id.trust_guide_layout:
			isTrust = true;
			mTrust.setSelected(true);
			if (isMIUI) {
				boolean isRight = XiaomiUtils.setTrust(this, mMIUIVersion);
				if(isRight){
					if (mMIUIVersion.equals("V5")) {
						startDialog(2);
//						XiaomiDialogActivity.startXiaomiDialogActivity(this, 2);
					} else if (mMIUIVersion.equals("V6")) {
						startSingleChoiceDialogActivity(getString(R.string.open_auto_run),getString(R.string.open_auto_run_up_dialog),getString(R.string.open_auto_run_dialog));
					} else if (mMIUIVersion.equals("V7")) {
						startSingleChoiceDialogActivity(getString(R.string.open_auto_run),getString(R.string.open_auto_run_up_dialog),getString(R.string.open_auto_run_dialog));
					}
				}else{
					completion();
				}
			}
			break;
		case R.id.close_systemlocker_layout:
			isCloseSystemLock = true;
			mSystemLocker.setSelected(true);
			if (isMIUI) {
				XiaomiUtils.closeSystemLocker(this, true);
				if (mMIUIVersion.equals("V5")) {
					startDialog(3);
//					XiaomiDialogActivity.startXiaomiDialogActivity(this, 3);
				} else if (mMIUIVersion.equals("V6")) {
					startSingleChoiceDialogActivity(getString(R.string.close_system_locker),getString(R.string.developer_options_up_dialog),getString(R.string.developer_options_dialog));
				} else if (mMIUIVersion.equals("V7")) {
					startSingleChoiceDialogActivity(getString(R.string.close_system_locker),getString(R.string.developer_options_up_dialog),getString(R.string.developer_options_dialog));
				}
			}

			break;

		case R.id.experience_btn:
			showTip();
			break;
		default:
			break;
		}
	}
	
	private void startDialog(int type){
		Intent intent = new Intent(this, XiaomiDialogActivity.class);
		// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Bundle b = new Bundle();
		b.putInt("miuiType", type);
		intent.putExtras(b);
		startActivityForResult(intent, 0);
	}
	
	private void startSingleChoiceDialogActivity(String title, String contentUp,String content) {
		Intent intent = new Intent(this, SingleChoiceDialogActivity.class);
//		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Bundle b = new Bundle();
		b.putString(SingleChoiceDialogActivity.CONTENT_UP, contentUp);
		b.putString(SingleChoiceDialogActivity.CONTENT, content);
		b.putString(SingleChoiceDialogActivity.TITLE, title);
		intent.putExtras(b);
		startActivityForResult(intent, 0);
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) { // 根据状态码，处理返回结果
		case RESULT_OK:
			completion();
			break;
		default:
			break;
		}
	}
	
	private void showTip() {
		SpecialSettingDialog dialog = new SpecialSettingDialog(this,getString(R.string.special_setting_completed),getString(R.string.special_setting_return));
		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(false);
		dialog.setTitle(getString(R.string.special_setting_tip))
				.setContent(getString(R.string.special_setting_tip_content))
				.setRedContent(getString(R.string.special_setting_tip_red_content))
				.setListener(new SpecialDialogListener() {
					@Override
					public void onConfirm() {
						LockPrefs.setStartLocker(SpecialSettingActivity.this, true);
						setResult(RESULT_OK);
						finish();
					}
				}).show();

	}

	private void completion() {
		if (isOpenFloatWindow) {
			mFirst.setSelected(true);
		}
		if (isTrust) {
			mSecond.setSelected(true);
		}
		if (isCloseSystemLock) {
			mThird.setSelected(true);
		}
		if (isCloseSystemLock && isOpenFloatWindow && isTrust) {
			mExperience.setSelected(true);
			mExperience.setClickable(true);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	
}